import { Command } from '../../../utils/commandManager';
import TypeScriptServiceClientHost from '../../../typeScriptServiceClientHost';
import { Lazy } from '../../../utils/lazy';
import { nulToken } from '../../../utils/cancellation';
import * as vscode from 'vscode';


export class 转换当前文件命令_ implements Command {
    public static readonly Id = 'ctsscript.转换当前文件命令_';
    public readonly ID = 转换当前文件命令_.Id;
    constructor(private client: Lazy<TypeScriptServiceClientHost>) { }
    public async execute() {
        vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: "正在转换" }, () => this.执行());
    }
    public async 执行() {
        const arg = { file: this.client.value.serviceClient.toOpenedFilePath(vscode.window.activeTextEditor!.document) };
        const 结果 = await this.client.value.serviceClient.execute("convertCtsFile", arg, nulToken);
        if (结果 && 结果.type === "response" && 结果.body) {
            const text = 结果.body;
            if (text) {
                vscode.window.activeTextEditor!.edit(e => {
                    e.replace(new vscode.Range(new vscode.Position(0, 0),
                        new vscode.Position(vscode.window.activeTextEditor!.document.lineCount - 1,
                            vscode.window.activeTextEditor!.document.lineAt(vscode.window.activeTextEditor!.document.lineCount - 1).text.length)),
                        text);
                });
            }
        }
        else {
            vscode.window.activeTextEditor!.revealRange
            vscode.window.showErrorMessage<vscode.MessageItem>("转换失败！", { title: "转换失败", isCloseAffordance: true });
        }
        return;
    }
}